package com.hqd.ch03.v46.boot.context.properties.bind;

import com.hqd.ch03.v46.boot.context.properties.source.ConfigurationProperty;

import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

public class UnboundConfigurationPropertiesException extends RuntimeException {

    private final Set<ConfigurationProperty> unboundProperties;

    public UnboundConfigurationPropertiesException(Set<ConfigurationProperty> unboundProperties) {
        super(buildMessage(unboundProperties));
        this.unboundProperties = Collections.unmodifiableSet(unboundProperties);
    }

    private static String buildMessage(Set<ConfigurationProperty> unboundProperties) {
        StringBuilder builder = new StringBuilder();
        builder.append("The elements [");
        String message = unboundProperties.stream().map((p) -> p.getName().toString()).collect(Collectors.joining(","));
        builder.append(message).append("] were left unbound.");
        return builder.toString();
    }

    public Set<ConfigurationProperty> getUnboundProperties() {
        return this.unboundProperties;
    }

}
